package customer.zspt.cn.customer.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

/**
 * Created by Administrator on 2018/7/24 0024.
 */

public class RequestPermissionsUtils {
    public static int REQUEST_EXTERNAL_STORAGE = 0;
    public static int REQUEST_CMAERA = 1;
    public static int RECORD_AUDIO = 2;
    //检测存储權限
    public  static boolean checkRWritePermission(final Activity activity) {
        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission
                    .WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(activity,"此權限对程序很重要",Toast.LENGTH_SHORT).show();
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        Intent intent = new Intent();
//                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                        intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
//                        activity.startActivity(intent);
//                    }
//                },500);
            }
            //申请权限
            ActivityCompat.requestPermissions(activity, new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    },
                    REQUEST_EXTERNAL_STORAGE);
                   return false;
        }

           return true;
    }
    //检测相机權限
    public static boolean checkCameraPermission(final Activity activity) {

        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission
                    .CAMERA)) {
                Toast.makeText(activity,"暂无此权限，请前往开通,不然无法正常使用功能",Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
                        activity.startActivity(intent);
                    }
                },500);
            }
            //申请权限
            ActivityCompat.requestPermissions(activity, new String[]{
                            Manifest.permission.CAMERA,
                    },
                    REQUEST_CMAERA);
                 return false;
        }
        return true;

    }
    //检测相机權限
    public static void checkAudioRecordPermission(final Activity activity) {

        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {
            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission
                    .CAMERA)) {
                Toast.makeText(activity,"暂无此权限，请前往开通,不然无法正常使用功能",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
                        activity.startActivity(intent);
                return;
            }
            //申请权限
            ActivityCompat.requestPermissions(activity, new String[]{
                            Manifest.permission.RECORD_AUDIO,
                    },
                    RECORD_AUDIO);

        } else {

        }
    }
}
